﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.auth.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.auth.dao.*;
import com.hh.bss.auth.entity.*;
import com.hh.bss.auth.service.*;
import com.hh.bss.auth.vo.query.*;
import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.util.*;




@Service
@Transactional
public class SystemBranchTitleServiceImpl extends BaseBusinessService<SystemBranchTitle,java.lang.Integer>  implements SystemBranchTitleService<SystemBranchTitle,java.lang.Integer>{

	private SystemBranchTitleDao systemBranchTitleDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setSystemBranchTitleDaoImpl(SystemBranchTitleDao dao) {
		this.systemBranchTitleDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.systemBranchTitleDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(SystemBranchTitleQuery query) {
		return systemBranchTitleDao.findPage(query);
	}
	public void save(SystemBranchTitle systemBranchTitle){
				
		systemBranchTitleDao.save(systemBranchTitle);
		systemBranchTitleDao.flush();
		List l=systemBranchTitleDao.getHibernateTemplate().find("from SystemBranchTitle t where t.id=?",systemBranchTitle.getParentId());
		
		if(l!=null&&l.size()>0) {
			SystemBranchTitle pSystemBranchTitle=(SystemBranchTitle)l.get(0);			
			systemBranchTitle.setParentIds(StringHelper.formatNullValue(pSystemBranchTitle.getParentIds(), "/")+systemBranchTitle.getId()+"/");
		}else 
			systemBranchTitle.setParentIds("/"+systemBranchTitle.getId()+"/");
		systemBranchTitleDao.update(systemBranchTitle);
		systemBranchTitleDao.flush();
	}
	public void removeById(Integer id){
		List l=systemBranchTitleDao.getHibernateTemplate().find("from SystemBranchTitle t where t.parentId=?",id);
		if(l!=null&&l.size()>0) throw new RuntimeException("岗位存在下级岗位,不能删除");
		l=systemBranchTitleDao.getHibernateTemplate().find("from SystemUserTitle t where t.titleId=?",id);
		if(l!=null&&l.size()>0) throw new RuntimeException("岗位存在员工,不能删除");
		systemBranchTitleDao.deleteById(id);
		systemBranchTitleDao.flush();
	}
	public List findSystemBranchTitle(SystemBranchTitleQuery query) {
		// TODO Auto-generated method stub
		StringBuffer sql=new StringBuffer();
		List values=new ArrayList();
		sql.append("from SystemBranchTitle t where 1=1");
		if (ObjectUtils.isNotEmpty(query.getDeptId())){
			//sql.append(" and t.deptId=? ");
			//values.add(query.getDeptId());
			sql.append(" and t.deptId in (").append(query.getDeptId()).append(")");
		}
		
		return systemBranchTitleDao.getHibernateTemplate().find(sql.toString(),values.toArray());
	}
	
}
